package com.ctg.payroll2.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.util.Assert;

import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.orm.ibatis.BaseQueryFilter;
import com.ctg.platform.util.Parse;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport {
	private static final long serialVersionUID = 4498248757573527481L;
	protected Log logger = LogFactory.getLog(this.getClass());
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	protected ServletContext context;
	protected SysUser currentUser;
	protected BaseQueryFilter filter;
	/**
	 * 页面操作标记
	 */
	private Integer actionType;
	private static final int NONE = -1;
	/**
	 * 查询操作标记
	 */
	public static final int SEARCH = 0;	
	/**
	 * 新增操作标记
	 */
	public static final int CREATE = 1;
	/**
	 * 删除操作标记
	 */
	public static final int DELETE = 2;
	/**
	 * 更新操作标记
	 */
	public static final int UPDATE = 3;
	/**
	 * 重新查找一遍实体（查看用）
	 */
	public static final int RETRIEVE = 4;
	/**
	 * 导出xls
	 */
	public static final int EXPORT = 5;
	public String execute() throws Exception{
		this.request = ServletActionContext.getRequest();
		this.response = ServletActionContext.getResponse();
		this.session = request.getSession();
		this.context = session.getServletContext();
		this.currentUser = (SysUser)session.getAttribute(Constants.CURRENT_USER);
		Assert.isTrue(currentUser!=null, "It looks like you have not login the Payroll system, relogin the system maybe resolve this problem !");
		logger.debug("从session中成功得到当前登陆用户:"+currentUser.getEmployeeName());
		actionType = actionType == null ? new Integer(0) : actionType;
		logger.debug("当前的操作标记actionType为:"+actionType.intValue());
		
		switch(actionType.intValue()){
			case CREATE:
				createEntity();
				logger.debug("执行了创建实体方法.");
				break;
			case DELETE:
				deleteEntity();
				logger.debug("执行了删除实体方法.");
				break;
			case UPDATE:
				updateEntity();
				logger.debug("执行了更新实体方法.");
				break;
			case SEARCH:
				query();
				logger.debug("执行了查询实体方法.");
				break;
			case RETRIEVE:
				retrieveEntity();
				logger.debug("执行了复查实体方法.");
				break;
			case EXPORT:
				exportXLS();
				logger.debug("执行了导出xls方法.");
				break;
			case NONE:
				nothingtodo();
				logger.debug("执行了nothing方法.");
				break;
			default:
				break;
		}
		otherExecute(actionType.intValue());
		//公共方法
		commonOperations();
		return SUCCESS;
	}
	private void query() {
		if (filter == null) {
			filter = new BaseQueryFilter();
		}
		filter.setStartIndex(Parse.toInteger(request.getParameter("start") , 0));
		filter.setEndIndex(Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		queryEntity();
	}
	/**
	 * 创建实体
	 *
	 */
	protected abstract void createEntity();
	/**
	 * 删除实体
	 *
	 */
	protected abstract void deleteEntity();
	/**
	 * 更新实体
	 * @throws Exception 
	 *
	 */
	protected abstract void updateEntity() throws Exception;
	/**
	 * 查询实体
	 *
	 */
	protected abstract void queryEntity();
	/**
	 * 复查实体
	 *
	 */
	protected abstract void retrieveEntity();
	/**
	 * 导出excel
	 *
	 */
	protected abstract void exportXLS();
	/**
	 * 什么都不做
	 *
	 */
	protected void nothingtodo(){};
	/**
	 * 通用操作
	 *
	 */
	protected abstract void commonOperations();
	/**
	 * 其他操作
	 * @param intValue
	 */
	protected void otherExecute(int intValue){
		
	}
	/**
	 * @return 返回actionType
	 */
	public Integer getActionType() {
		return actionType;
	}
	/**
	 * @param actionType 设置actionType
	 */
	public void setActionType(Integer actionType) {
		this.actionType = actionType;
	}
}
